<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%--
  Created by IntelliJ IDEA.
  User: Wangliwei
  Date: 2017/6/18
  Time: 21:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<%if(session.getAttribute("uname")!=null)
{// 用户已登陆%>
<%! String DBDRIVER			= "com.mysql.jdbc.Driver" ;
  String DBURL			= "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8" ;
  String DBUSER			= "root" ;
  String DBPASSWORD		= "" ;
  Connection conn			= null ;
  PreparedStatement pstmt	= null ;	%>
<%// 声明一个boolean变量
  boolean flag = false ; // 接收参数
  String num = request.getParameter("num");
  String name = request.getParameter("name");
  String sex = request.getParameter("sex") ;
  String score = request.getParameter("score") ;
  String indate = request.getParameter("indate") ;
%>
<%
  String sql = "  insert into students (num,name,sex,score,indate) values(?,?,?,?,?)   " ;
  try{Class.forName(DBDRIVER) ;
    conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    pstmt = conn.prepareStatement(sql) ;
    pstmt.setString(1,num);
    pstmt.setString(2,name);
    pstmt.setString(3,sex);
    pstmt.setString(4,score);
    pstmt.setString(5,indate);
    pstmt.executeUpdate() ; pstmt.close() ; conn.close() ;
    flag = true ; }catch(Exception e) {}%>

<%response.setHeader("refresh","2;URL=main.jsp") ;
  if(flag) {%>信息添加成功，两秒后跳转到功能列表页！！！<br>
如果没有跳转，请按<a href="main.jsp">这里</a>！！！
<%}else{%>信息添加失败，两秒后跳转到信息列表页！！！<br>
如果没有跳转，请按<a href="main.jsp">这里</a>！！！
<%}%>
<%}else{// 用户未登陆，提示用户登陆，并跳转
  response.setHeader("refresh","2;URL=Login.jsp") ; %>
您还未登陆，请先登陆！！！<br>
两秒后自动跳转到登陆窗口！！！<br>
如果没有跳转，请按<a href="Login.jsp">这里</a>！！！<br>
<%}%>

</body>
</html>
